
from .Class_SVDObject import *
from xml.dom.minidom import Document as XMLFile



class EnumeratedValue(SVDObject):
    def __init__(self, enumValueName: str = "", enumDescription: str = "", enumValue: str = "", father: str = ""):
        super().__init__(enumValueName)
        self.type = "EnumeratedValue"
        self.enumValueName = enumValueName
        self.father = father
        self.enumDescription = enumDescription 
        self.enumvalue = enumValue
        
    def SetValue(self, enumValue: str):
        self.enumvalue = enumValue

    def SetFather(self, father: str):
        self.father = father
    
    def SetDescription(self, enumDescription: str):
        self.enumDescription = enumDescription

    def __repr__(self):
        return f"EnumeratedValue(enumValueName={self.enumValueName}, father={self.father}, enumDescription={self.enumDescription}, enumValue={self.enumValue})"

    def toString(self, tablevel) -> str:
        if self.enumValueName and self.enumvalue:
            xml_output = (
                f"{self.TabsString(tablevel)}<enumeratedValue>\n"
                f"{self.TabsString(tablevel+1)}<name>{self.enumValueName}</name>\n"
            )
            
            # 仅在 enumDescription 不为空时添加这一行
            if self.enumDescription :
                xml_output += f"{self.TabsString(tablevel+1)}<description>{self.enumDescription}</description>\n"
            
            xml_output += (
                f"{self.TabsString(tablevel+1)}<value>{self.enumvalue}</value>\n"
                f"{self.TabsString(tablevel)}</enumeratedValue>\n"
            )
        else :
            xml_output = ""
        
        return xml_output
    
    def toXmlObj(self, xmlfather):
        doc = xmlfather.ownerDocument
        
        if self.enumValueName != "" and self.enumvalue != "":
            enumerated_value_element = doc.createElement('enumeratedValue')
            
            name_element = doc.createElement('name')
            name_text = doc.createTextNode(self.enumValueName)
            name_element.appendChild(name_text)
            enumerated_value_element.appendChild(name_element)
            
            if self.enumDescription:
                description_element = doc.createElement('description')
                description_text = doc.createTextNode(self.enumDescription)
                description_element.appendChild(description_text)
                enumerated_value_element.appendChild(description_element)
            
            value_element = doc.createElement('value')
            value_text = doc.createTextNode(str(self.enumvalue))
            value_element.appendChild(value_text)
            enumerated_value_element.appendChild(value_element)
            
            xmlfather.appendChild(enumerated_value_element)

    def ValidCheck(self):        
        errcnt = 0
        if (self.enumValueName is not None and self.enumValueName != "") and (self.enumvalue is not None and self.enumvalue != ""):
            return super().ValidCheck()
        elif (self.enumValueName is None or self.enumValueName == "") and (self.enumvalue is None or self.enumvalue == ""):
            return 0
        else:
            print(f"[Error][{self.myself()}]is not valid, need both enum enumValue and name")
            errcnt = 1
        return errcnt
    
    @staticmethod
    def GetTestObject():
        return EnumeratedValue(enumValueName="NO_INV", enumDescription="Received data is not inverted.", enumValue="0")
    
    @staticmethod
    def TestString():
        enumerated_value = EnumeratedValue.GetTestObject()
        print(enumerated_value.toString(8))